package com.party.admin.biz.dbAnalyze;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.party.admin.web.dto.output.dbAnalyze.DbAnalyzeOutput;
import com.party.common.paging.Page;
import com.party.common.utils.DateUtils;
import com.party.common.utils.LangUtils;
import com.party.core.model.dbAnalyze.DbAnalyze;
import com.party.core.service.dbAnalyze.impl.DbAnalyzeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 数据分页业务服务
 * Created by wei.li
 *
 * @date 2017/8/3 0003
 * @time 10:55
 */

@Service
public class DbAnalyzeBizService {
    //时间参数键
    private final static String DAY = "day";
    //总数参数键
    private final static String COUNT = "count";

    private static final Integer END_DATE_INDEX = 14;

    private static final Integer START_DATE_INDEX = 0;

    @Autowired
    private DbAnalyzeService dbAnalyzeService;


    /**
     * 数据分析列表
     *
     * @param dbAnalyze 数据分析
     * @param page 分页参数
     * @return 数据分析列表
     */
    public List<DbAnalyzeOutput> list(DbAnalyze dbAnalyze, Page page){
        List<DbAnalyze> list = dbAnalyzeService.tables(dbAnalyze, page);
        List<DbAnalyzeOutput> dbAnalyzeOutputList
                = LangUtils.transform(list, new Function<DbAnalyze, DbAnalyzeOutput>() {
            @Override
            public DbAnalyzeOutput apply(DbAnalyze dbAnalyze) {
                DbAnalyzeOutput dbAnalyzeOutput = DbAnalyzeOutput.transform(dbAnalyze);
                boolean hasFile = dbAnalyzeService.hasFile(dbAnalyze.getTableName());
                if (hasFile){
                    Integer today = dbAnalyzeService.todayCount(dbAnalyze.getTableName());
                    Integer week = dbAnalyzeService.weekCount(dbAnalyze.getTableName());
                    Integer month = dbAnalyzeService.monthCount(dbAnalyze.getTableName());
                    dbAnalyzeOutput.setToday(today);
                    dbAnalyzeOutput.setWeek(week);
                    dbAnalyzeOutput.setMonth(month);
                }
                return dbAnalyzeOutput;
            }
        });
        return dbAnalyzeOutputList;
    }


    /**
     * 时间轴列表
     *
     * @return 时间轴列表
     */
    public List<String> listDay(){
        List<String> list = Lists.newArrayList();
        for (int i = END_DATE_INDEX; i >= 0; i --){
            String day = DateUtils.formatDate(DateUtils.getNextNDay(new Date(), -i));
            list.add(day);
        }
        return list;
    }

    /**
     * 统计一段时间的数据
     *
     * @param dayList 日期列表
     * @param countList 统计数据
     * @return 统计数据列表
     */
    public List<Integer> getCount(List<String> dayList, List<HashMap<String, Integer>> countList){
        List<Integer> resultList = Lists.newArrayList();
        for (String day : dayList){
            Integer num = 0;
            for (HashMap count : countList){
                if (day.equals(count.get(DAY))){
                    num = ((Long) count.get(COUNT)).intValue();
                    break;
                }
            }
            resultList.add(num);
        }
        return resultList;
    }


    /**
     * 统计列表
     *
     * @param tableName 表名
     * @return 统计数据
     */
    public List<Integer> countList(String tableName){

        List<String> dayList = this.listDay();
        List<HashMap<String, Integer>> list
                = dbAnalyzeService.countList(dayList.get(START_DATE_INDEX), dayList.get(END_DATE_INDEX), tableName);
        return this.getCount(dayList, list);
    }

}
